package com.gopay.bis.web.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bank.ScanPayBaseMessage;
import com.gopay.common.domain.bank.spdb.agpay.MicroPayBody;
import com.gopay.common.domain.bank.spdb.agpay.ScanPayBody;
import com.gopay.common.domain.bank.spdb.agpay.SdkPayBody;
import com.gopay.remote.bank.MicroPayInteractRemoteService;
import com.gopay.remote.bank.ScanPayInteractRemoteService;

@Controller
@RequestMapping(value = "/spdb/wechat")
public class SpdbWechatTest {
	private static Logger logger = LoggerFactory.getLogger(SpdbWechatTest.class);
	
	@Resource(name = "WECHAT-SCAN-SPDB")
	private ScanPayInteractRemoteService wechatSpdbScan;
	@Resource(name = "WECHAT-SDK-SPDB")
	private ScanPayInteractRemoteService wechatSpdbSdk;
	@Resource(name = "WECHAT-MICRO-SPDB")
	private MicroPayInteractRemoteService microSpdbPay;
	
	private static long seed = 0;
	
	@RequestMapping(value = "/pay", method = { RequestMethod.GET })
	public void pay(HttpServletRequest request, HttpServletResponse response) {
		String operater = request.getParameter("operator");
		logger.error("=============operater:" + operater + "===================");
		ScanPayBaseMessage scanPayMsg = new ScanPayBaseMessage();
		scanPayMsg.setDeviceInfo("info");
		scanPayMsg.setMchCreateIp("127.0.0.1");
		scanPayMsg.setLimitCreditPay("0");
		scanPayMsg.setProductId("001");
		scanPayMsg.setGoodsBody("goapy-wechat");
		scanPayMsg.setGoodsTag("test");
		scanPayMsg.setOrderDate("20160811053947");
		scanPayMsg.setAmount(new BigDecimal("0.01"));
		scanPayMsg.setOrderId(creatSerialNumber(16));
		scanPayMsg.setOrderId(creatSerialNumber(16));
		scanPayMsg.setBankIntrChann(BisBankInterfaceChannel._15);
		scanPayMsg.setBank("WECHAT");
		scanPayMsg.setOutStlmCd("WECHAT");
		scanPayMsg.setPayUrl("https://pay.swiftpass.cn/pay/gateway");
		scanPayMsg.setQueryUrl("https://pay.swiftpass.cn/pay/gateway");
		scanPayMsg.setRefundUrl("https://pay.swiftpass.cn/pay/gateway");
		scanPayMsg.setRefundQueryUrl("https://pay.swiftpass.cn/pay/gateway");
		try {
			if ("sdk".equals(operater)) {
				//sdk app支付
				//SdkPayBody body = new SdkPayBody();
				wechatSpdbSdk.pay(scanPayMsg);
			}else if("scan".equals(operater)){
				//scan 扫码支付 预下单支付 顾客主动扫
				//ScanPayBody body = new ScanPayBody();
				wechatSpdbScan.pay(scanPayMsg);
			}else if("micro".equals(operater)){
				//micro 条码支付 下单支付 顾客被扫
				MicroPayBody body = new MicroPayBody(); 
				microSpdbPay.pay(body);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String creatSerialNumber(int length) {
		byte[] randomSerial = new byte[length];
		String charactors = "123456789";
		int charSetLength = charactors.length();
		seed = seed ^ System.currentTimeMillis();
		java.util.Random randomNumber = new java.util.Random(seed);

		for (int i = 0; i < length; i++) {
			randomNumber.setSeed(seed);
			int j = (int) (randomNumber.nextFloat() * charSetLength);
			randomSerial[i] = (byte) charactors.charAt(j);
			seed = seed ^ randomNumber.nextInt();
		}
		return (new String(randomSerial));
	}
}
